package com.tywj.buscustomerapp.view.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.SearchUtilsBean;
import com.tywj.buscustomerapp.model.bean.TravelLineBean;
import com.tywj.buscustomerapp.presenter.BasePresenter;
import com.tywj.buscustomerapp.presenter.SearchPresenter;
import com.tywj.buscustomerapp.presenter.contract.SearchContract;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.myadapter.CommonRcvAdapter;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.item.SearchItem;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.tywj.buscustomerapp.view.myview.ToastJoe;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

public class SearchActivity extends BaseActivityMvp implements SearchContract.SearchView {
    @BindView(R.id.head_left)
    ImageView back;
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.search_result)
    RecyclerView recyclerView;
    @BindView(R.id.no_line)
    LinearLayout noLine;
    @BindView(R.id.start_no)
    TextView start_no;
    @BindView(R.id.end_no)
    TextView end_no;
    @BindView(R.id.cs_submit)
    TextView submit;

    private List<SearchUtilsBean> lines = new ArrayList<>();
    private CommonRcvAdapter<SearchUtilsBean> adapter;
    private SearchContract.SearchPersenter persenter = new SearchPresenter(this);
    private double[] address;
    private LoadingDialog waitDialog;



    @Override
    public int getLayoutResId() {
        return R.layout.activity_search;
    }

    @Override
    public void initView() {
        title.setText("搜索查询结果");
        if (getIntent()!=null){
            address = getIntent().getDoubleArrayExtra("start&end");
        }
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        back.setOnClickListener(this);
        submit.setOnClickListener(this);
        adapter = new CommonRcvAdapter<SearchUtilsBean>(lines) {
            @NonNull
            @Override
            public AdapterItem createItem(Object type) {
                return new SearchItem();
            }
        };
        recyclerView.setAdapter(adapter);
        persenter.startGetStation(SPUtils.getCity(getApplicationContext()).getCityId(),
                new LatLng(address[0],address[1]),new LatLng(address[2],address[3]));
    }

    @Override
    public void initData() {
//        adapter.setData(lines);
//        adapter.notifyDataSetChanged();
    }

    @Override
    protected BasePresenter bindPresenter() {
        return persenter;
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.head_left:
                finish();
                break;
            case R.id.cs_submit:
                Intent intent = new Intent(SearchActivity.this, ApplyActivity.class);
                intent.putExtra("type",0);
                intent.putExtra("address",address);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showSearchResoult(List<SearchUtilsBean> beans) {
        if (beans!=null){
            adapter.setData(beans);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void startLoading() {
        if (waitDialog==null)
            waitDialog = new LoadingDialog(SearchActivity.this);
        if (!waitDialog.isShowing())
            waitDialog.show();
    }

    @Override
    public void closeLoading() {
        if (waitDialog!=null && waitDialog.isShowing())
            waitDialog.dismiss();
    }

    @Override
    public void showError(String message) {
        ToastJoe.getmToastJoe().ToastLongShow(SearchActivity.this,null,message);
        noLine.setVisibility(View.VISIBLE);
        start_no.setText(SPUtils.getStartAndEnd(getApplicationContext())[0]);
        end_no.setText(SPUtils.getStartAndEnd(getApplicationContext())[1]);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(waitDialog!=null){
            waitDialog.dismiss();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("搜索");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("搜索");
        MobclickAgent.onPause(this);
    }
}
